package navigation.results.cluster;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

public class ClusterReport {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		String navFolder = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/NavigationClusterComplexTextNext/";
		
		File outp = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/NavigationReportComplexTextNext/");
		
		File fold = new File(navFolder);
		
		File[] navfolders = fold.listFiles();
		
		ArrayList<String> good = new ArrayList<String>();
		
		ArrayList<String> bad = new ArrayList<String>();
		
		for (int i = 0; i < navfolders.length; i++) {
			
			System.out.println("DATABASE: " + i);
			
			File folder = navfolders[i];
			
			String name = folder.getName();
			
			File[] files = folder.listFiles(new FileFilter() {
				
				@Override
				public boolean accept(File pathname) {
					return pathname.getAbsolutePath().endsWith(".html");
				}
			});
			
			File results = new File(folder, "notFound.txt");
			
			int howmany = getNoCluster(results);
			
			if (files.length == howmany){
				bad.add(name);
			}else{
				good.add(name);
			}
			
				
		}

		write(bad,new File(outp,"bad"));
		
		write(good,new File(outp,"good"));
		
	}

	private static void write(ArrayList<String> array, File file) throws IOException {
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		
		for (String string : array) {
			bw.write(string + "\n");
		}
		
		bw.close();
	}

	private static int getNoCluster(File results) throws IOException {
		
		int ret = 0;
		
		BufferedReader br = new BufferedReader(new FileReader(results));
		
		String line;
		
		while ((line=br.readLine())!=null){
			
			if (line.startsWith("/") && line.endsWith(".html")){
				ret++;
			}
			
		}
		
		br.close();
		
		return ret;
	}

}
